package com.gw.mapper;

import com.github.pagehelper.Page;
import com.gw.pojo.entity.Post;
import com.gw.pojo.entity.Reply;
import com.gw.pojo.vo.PostVO;
import com.gw.pojo.vo.ReplyVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper {

    /**
     * 发帖
     * @param post
     */
    @Insert("insert into post (host_id, title, `desc`, image) " +
            "values (#{hostId}, #{title}, #{desc}, #{image})")
    void addPost(Post post);

    /**
     * 插入评论回复
     * @param reply
     */
    @Insert("insert into reply (post_id, replier_id, content, replied_id) " +
            "values (#{postId}, #{replierId}, #{content}, #{repliedId})")
    void addReply(Reply reply);

    @Delete("delete from post where id = #{postId}")
    void deleteById(Long postId);

    /**
     * 分页查询帖子
     * @param page
     * @param size
     * @return
     */
    Page<PostVO> pagePost(int page, int size);


    /**
     *评论数++
     * @param postId
     */
    @Update("update post set commen_number = commen_number+1 where id = #{postId}")
    void addCommentNumber(Long postId);

    /**
     * 根据id查询帖子
     * @param postId
     * @return
     */
    @Select("select * from post where id = #{postId}")
    Post getPostById(long postId);

    /**
     * 查询帖子的所有评论
     * @param postId
     * @return
     */
    List<ReplyVO> getReplyByPostId(long postId);

    /**
     * 根据hostId查询帖子
     * @param page
     * @param size
     * @param hostId
     * @return
     */
    Page<PostVO> pagePostById(int page, int size, Long hostId);

    @Delete("delete from reply where post_id = #{postId}")
    int deleteAllReplyByPostId(Long postId);

    @Delete("delete from post where id=#{postId};")
    int deletePostById(Long postId);

}
